package com.yowasa.xiuxian.engine.message.item;

import cn.hutool.core.lang.WeightRandom;
import cn.hutool.core.util.RandomUtil;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.google.common.collect.Lists;
import com.yowasa.xiuxian.annoations.CMD;
import com.yowasa.xiuxian.engine.CommonEngine;
import com.yowasa.xiuxian.engine.item.ItemInterface;
import com.yowasa.xiuxian.engine.message.CmdMessageHandler;
import com.yowasa.xiuxian.entity.bo.user.UserBO;
import com.yowasa.xiuxian.entity.enums.CmdScopeEnum;
import com.yowasa.xiuxian.entity.mys.MysComponent;
import com.yowasa.xiuxian.entity.mys.SendMessage;
import com.yowasa.xiuxian.entity.po.XiuXianExtend;
import com.yowasa.xiuxian.entity.po.XiuxianPrayerLog;
import com.yowasa.xiuxian.exception.HandlerException;
import com.yowasa.xiuxian.service.helper.*;
import com.yowasa.xiuxian.util.ComponentUtil;
import org.springframework.beans.factory.annotation.Autowired;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@CMD(name = "/祈愿", scope = CmdScopeEnum.ALL)
public class PrayerHandler extends CmdMessageHandler {
    @Autowired
    private MessageHelper messageHelper;
    @Autowired
    private UserHelper userHelper;
    @Autowired
    private ItemHelper itemHelper;
    @Autowired
    private ExtendHelper extendHelper;
    @Autowired
    private PrayerHelper prayerHelper;

    @Override
    public void execute(SendMessage message) {
        UserBO user = userHelper.checkOpUser(message);
        List<String> params = getAllParam(message);

        if (StringUtils.isEmpty(params.get(0))) {
            MysComponent component = new MysComponent("1",2,"鸣雷寂灭","/祈愿 鸣雷寂灭");
//            MysComponent componentA = new MysComponent("2",2,"赤团开时","/祈愿 赤团开时");
//            List<List<MysComponent>> lists = ComponentUtil.genComponentList(2, component, componentA);
            List<List<MysComponent>> lists = ComponentUtil.genComponentList(2, component);
//            messageHelper.replyMysPanel(message, "可以在后面追加 鸣雷寂灭 记录 等二级指令进行后续操作\n华紫绯樱UP武器 ： 雾切之回光  神乐之真意\n赤团开时UP武器 ： 狼的末路    护摩之杖",null,lists,null);
            messageHelper.replyMysPanel(message, "可以在后面追加 鸣雷寂灭 记录 等二级指令进行后续操作\n鸣雷寂灭UP武器 ： 飞雷之弦振  薙草之稻光",null,lists,null);

            return;
        }
//        if ("华紫绯樱".equals(params.get(0))) {
//            randomPrayer("雾切之回光","神乐之真意",user,message);
//        }
//        if ("赤团开时".equals(params.get(0))) {
//            randomPrayer("狼的末路","护摩之杖",user,message);
//        }
        if ("鸣雷寂灭".equals(params.get(0))) {
            randomPrayer("飞雷之弦振","薙草之稻光",user,message);
        }
        if ("记录".equals(params.get(0))) {

            int num = prayerHelper.getTimes(user);
            List<XiuxianPrayerLog> prayerLogs = new ArrayList<>();
            if(params.size() == 1){
                prayerLogs = prayerHelper.selectSize(user,1);
            }else {
                int i;
                try {
                    i = Integer.parseInt(params.get(1));
                }catch (Exception e){
                    i=1;
                }
                prayerLogs = prayerHelper.selectSize(user,i);
            }
            StringBuffer sb = new StringBuffer();
            sb.append("当前已经祈愿次数：").append(num).append("\n默认一次查询10条记录，可使用 /祈愿 记录 页码  指定查询第几页\n\n").append("时间                  物品\n");
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            prayerLogs.forEach(e -> {
                sb.append(sdf.format(e.getCreateTime())).append("   ").append(e.getItemName()).append("\n");
            });
            messageHelper.replyMys(message,sb.toString());
        }


    }

    void randomPrayer(String firstOne, String secondOne, UserBO user, SendMessage message){
        XiuXianExtend byAccountId = extendHelper.getByAccountId(user.getId());
        if(byAccountId.getRough() < 100){
            throw new HandlerException("原石不足100，无法祈愿");
        }else {
            byAccountId.setRough(byAccountId.getRough() - 100);
            byAccountId.setStarlight(byAccountId.getStarlight() + 10);
            extendHelper.updateExtendById(byAccountId);
        }

        double random = Math.random();
        int quality = RandomUtil.randomInt(6) + 1;
        int num = prayerHelper.getTimes(user);
        double flag ;
        if(num < 23){
            flag = 0.005;
        }else {
            flag = 0.005 + 0.02*(num-20);
        }
        if(random < flag || num >= 50){
            String itemName = RandomUtil.randomEle(Lists.newArrayList(firstOne,secondOne));
            ItemInterface itemInterface = CommonEngine.ITEM_MAP.get(itemName);
            itemHelper.addEquip(user,itemInterface,1,1);
            messageHelper.replyMys(message,"命定的祈愿绽放耀眼的金光！\n你获得了["+itemName+"]*1,请使用 /道具 列表 查看");
            prayerHelper.insert(user,itemName,1);
        }else {
            ArrayList<String> first = Lists.newArrayList("无锋剑", "魔导绪论", "故人之心", "异国之盏", "感别之冠", "逐光之石", "归乡之羽");
            ArrayList<String> second = Lists.newArrayList("暗巷闪光","流浪乐章","角斗士的留恋","角斗士的希冀","角斗士的凯旋","角斗士的酣醉","角斗士的归宿");
            ArrayList<String> third = Lists.newArrayList("天空之刃","四风原典","乐团的晨光","终幕的时计","指挥的礼帽","吟游者之壶","琴师的箭羽");
            ArrayList<String> origin = Lists.newArrayList("北陆武器原胚","北陆法器原胚","生之花","时之沙","理之冠","空之杯","死之羽");
            Map<String, List<String>> map =new HashMap<>();
            map.put("first",first);
            map.put("second",second);
            map.put("third",third);
            map.put("origin",origin);


            List<WeightRandom.WeightObj<String>> list = new ArrayList<WeightRandom.WeightObj<String>>();
            //一级装备
            list.add(new WeightRandom.WeightObj<>("first",85));
            //二级装备
            list.add(new WeightRandom.WeightObj<>("second",20));
            //三级装备
            list.add(new WeightRandom.WeightObj<>("third",5));
            //原胚
            list.add(new WeightRandom.WeightObj<>("origin",10));
            WeightRandom<String> stringWeightRandom = RandomUtil.weightRandom(list);
            List<String> strings = map.get(stringWeightRandom.next());
            String s = RandomUtil.randomEle(strings);
            ItemInterface itemInterface = CommonEngine.ITEM_MAP.get(s);
            if(itemInterface.getType().getId() == 3){
                itemHelper.addEquip(user,itemInterface,1,quality);
            }else {
                itemHelper.addItem(user,itemInterface,1);
            }
            prayerHelper.insert(user,s,2);
            messageHelper.replyMys(message,"你获得了["+s+"]*1,请使用 /道具 列表 查看");

        }

    }

    public String desc(){
        return "";
    }
}
